package com.atguigu.ai.all.config;

import com.atguigu.ai.all.func.RecruitServiceFunction;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.model.function.FunctionCallback;
import org.springframework.ai.reader.TextReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;

import java.util.List;
import java.util.function.Function;

@Configuration
public class RagConfig {

    @Bean
    VectorStore vectorStore(EmbeddingModel embeddingModel) {
        SimpleVectorStore simpleVectorStore =
                SimpleVectorStore.builder(embeddingModel).build();
        //提取文本内容
        String filepath = "张三简历.txt";
        TextReader textReader = new TextReader(filepath);
        textReader.getCustomMetadata().put("filepath",filepath);
        List<Document> documents = textReader.get();

        //段落切分
        TokenTextSplitter splitter =
                new TokenTextSplitter(1200,
                        350, 5,
                        100, true);
        splitter.apply(documents);

        //向量化存储
        simpleVectorStore.add(documents);
        return simpleVectorStore;
    }

    /**
     * 创建招聘服务函数回调Bean
     * 该函数用于构建一个函数回调对象，用于判断某某是否有资格面试
     *
     * @param recruitServiceFunction 招聘服务函数实现类，提供具体的业务逻辑处理
     * @return FunctionCallback 函数回调对象，包含函数名称、描述、输入类型等信息
     */
    @Bean
    @Description("某某是否有资格面试")
    public FunctionCallback recruitServiceFunctionCallback(RecruitServiceFunction recruitServiceFunction) {
        // 构建函数回调对象，指定函数名称、实现类、描述和输入类型
        return FunctionCallback.builder()
                .function("recruitServiceFunction", recruitServiceFunction)
                .description("某某是否有资格面试")
                .inputType(RecruitServiceFunction.Request.class)  // 显式指定输入类型
                .build();
    }

}
